package mephisto.services.impl;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;

import mephisto.Context;
import mephisto.Mephisto;
import mephisto.Constants;
import mephisto.services.IStorageService;

public class FileStorageService extends BaseService implements IStorageService {

    public void store(String content, String uuid, Context context) {

        String rootPath = getRootPath();
        File f = new File(rootPath + uuid);
        FileWriter fw;
        try {
            fw = new FileWriter(f);
            BufferedWriter bw = new BufferedWriter(fw);
            bw.write(content);
            bw.flush();
            fw.flush();
            fw.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        

    }

    private String getRootPath() {
        String rootPath = (String) Mephisto.getInstance().get(Constants.STORAGE_ROOT);
        if (rootPath.charAt(rootPath.length() - 1) != File.separatorChar)
            rootPath = rootPath + File.separatorChar;
        return rootPath;

    }

    public InputStream get(String uuid, Context context) {
        File f = new File(getRootPath() + uuid);
        FileInputStream fis = null;
        try {
            fis = new FileInputStream(f);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        return fis;

    }


//    public StorageService(Context context, Request request, Response response) {
//        super(context, request, response);
//        Method m = request.getMethod();
//        getVariants().add(new Variant(MediaType.TEXT_PLAIN));
//
//    }
//
//    public void put(Representation entity) {
//        if (entity != null) {
//
//            if (MediaType.APPLICATION_WWW_FORM.equals(entity.getMediaType(), true)) {
//
//                try {
//                    Form form = getRequest().getEntityAsForm();
//
//                    String content = form.getFirstValue("content");
//                    String contentId = (String) getRequest().getAttributes().get("content-id");
//                    if (authenticate(form)) {
//                        store(content, contentId);
//                    } else {
//
//                    }
//
//                    Representation rep = new StringRepresentation("result", MediaType.TEXT_PLAIN);
//                    getResponse().setEntity(rep);
//                    getResponse().setStatus(Status.SUCCESS_OK);
//
//                } catch (Exception e) {
//                    getResponse().setEntity(new StringRepresentation(e.getMessage(), MediaType.TEXT_PLAIN));
//                    getResponse().setStatus(Status.CLIENT_ERROR_BAD_REQUEST);
//                    e.printStackTrace();
//
//                }
//            }
//
//        }
//
//    }
//
//    public Representation represent(Variant variant) throws ResourceException {
//        if (variant.getMediaType().equals(MediaType.TEXT_PLAIN)) {
//
//            String contentId = (String) getRequest().getAttributes().get("content-id");
//            String path = getRequest().getOriginalRef().getPath();
//
//            InputStream is = getContent(contentId);
//
//            return new InputRepresentation(is, MediaType.ALL);
//
//        }
//
//        String s = "Swim service. Protocol version: 0.1";
//        return new StringRepresentation(s);
//
//    }

}
